﻿<%@ WebHandler Language="C#" Class="ShowRemarkInfo" %>

using System;
using System.Web;
using System.Text;
using System.Data;
using Chenxin.Lib.Utility;
using Chenxin.Lib.Business;
using Chenxin.Lib.Business.BLL;

public class ShowRemarkInfo : AjaxHandlerBase
{
    /// <summary>
    /// 基本权限项
    /// </summary>
    public ShowRemarkInfo()
    {
        //合同备注登记
        htTypePermit.Add("contractremark", Constants.Permit_ContractRemark);
        //作废成交记录
        htTypePermit.Add("addcancelremark", Constants.Permit_Bargain_CancelRemark);
        //未收佣收跟进
        htTypePermit.Add("addcommision", Constants.Permit_CommisionRemark);
    }
    
    override public void RequestProcess(HttpContext context)
    {
        context.Response.AddHeader("pragma", "no-cache");
        context.Response.AddHeader("cache-control", "");
        context.Response.CacheControl = "no-cache";
        string strOp = context.Request["op"];
        string strResponse = "";
        switch (strOp.ToLower())
        {
            //查询
            case "searchremark":
                strResponse = Search(context);
                break;
            //添加
            case "addremark":
                strResponse = Apply(context);
                break;
            //未收佣收跟进
            case "addcommision":
                strResponse = AddCommision(context);
                break;
            //作废成交记录跟进
            case "addcancelremark":
                strResponse = AddCancelRemark(context);
                break;
            default:
                break;
        }
        context.Response.Write(strResponse);
    }

    //未收佣收跟进
    //tianno
    //2010-11-3
    private string AddCommision(HttpContext context)
    {
        //未收佣收信息
        string remarkinfo = context.Request.Params["model"];

        //合同备注信息业务类
        Chenxin.Lib.Business.BLL.ContractRemark bllContractRemark = new Chenxin.Lib.Business.BLL.ContractRemark();

        //未收佣收信息
        Chenxin.Lib.Business.Entity.t_Contract_RemarkInfo entContractRemark =
            (Chenxin.Lib.Business.Entity.t_Contract_RemarkInfo)JsonHelp.Convert2Entity(remarkinfo,
                    typeof(Chenxin.Lib.Business.Entity.t_Contract_RemarkInfo));

        entContractRemark.RecordId = Guid.NewGuid();
        entContractRemark.RemarkDate = DateTime.Now;
        entContractRemark.RemarkPersonId = CurrentUserInfo.UserId;
        entContractRemark.RemarkPerson = CurrentUserInfo.UserName;

        //更新合同——跟进字段
        Chenxin.Lib.Business.Entity.t_Contract_BaseInfo baseinfo = Chenxin.Lib.Business.DAL.t_Contract_BaseInfo.GetModel(entContractRemark.ContractId);
        baseinfo.HasFollow = true;
        
        //未收佣收信息录入
        bllContractRemark.Add(entContractRemark);
        Chenxin.Lib.Business.DAL.t_Contract_BaseInfo.Update(baseinfo);
        return retExecResult(Constants.ExecResult.Succ, "未收佣收跟进成功！", "");
    }

    //*******************************************
    //功能概要       ：获得合同备注一览
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：获得合同备注
    //作成者         ：zou.xiaochun
    //作成日         ：2010-11-01
    //*******************************************
    private string Search(HttpContext context)
    {
        try
        {
            //页码
            int strPageIndex = (string.IsNullOrEmpty(context.Request["pageindex"])) ? 1 : int.Parse(context.Request["pageindex"]) + 1;
            //页显示行数
            int strPageSize = (string.IsNullOrEmpty(context.Request["pagesize"])) ? 10 : int.Parse(context.Request["pagesize"]);
            //查询条件
            string sqlWhere = GenerateCondition(context);
            
            //查询分页信息
            Chenxin.Lib.Business.BLL.ContractRemark bllContractRemark = new Chenxin.Lib.Business.BLL.ContractRemark();
            System.Data.DataSet ds = bllContractRemark.getRemarkList(sqlWhere, strPageIndex, strPageSize);
            //总页数
            int count = (int)ds.Tables[1].Rows[0][0];

            return string.Format("{{success:'1',msg:'{0}',data:{{count:'{1}',{2}}}}}", Resources.Message.Global_Msg_SUCCESS, count, JsonHelp.Convert2Json(ds.Tables[0]));
        }
        catch (Exception ex)
        {
            return string.Format("{{success:'0',msg:'{0}：{1}',data:''}}", Resources.Message.Global_Err_Exception, ex.Message);
        }
    }

    //*******************************************
    //功能概要       ：查询权限
    //参数説明       ：context 请求上下文
    //返回值説明     ：程序处理信息提示
    //功能詳細       ：生成查询条件语句
    //作成者         ：chen.yugong
    //作成日         ：2010-06-29
    //修改者         ：王玮
    //修改日         ：2010-07-13
    //*******************************************
    public string GenerateCondition(HttpContext context)
    {
        try
        {
            string contractid = context.Request["modelid"];
            System.Text.StringBuilder sb = new System.Text.StringBuilder();
            sb.Append(" (1=1) ");
            sb.AppendFormat(" and ContractId = '{0}'", contractid);
            return sb.ToString();
        }
        catch (Exception ex)
        {
            return string.Format("{{success:'0',msg:'{0}：{1}',data:''}}", Resources.Message.Global_Err_Exception, ex.Message);
        }
    }

    //*******************************************
    //功能概要       ：合同备注
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：合同备注
    //作成者         ：zou.xiaochun
    //作成日         ：2010-11-01
    //*******************************************
    private string Apply(HttpContext context)
    {
        //合同备注信息
        string remarkinfo = context.Request.Params["model"];

        //合同备注信息业务类
        Chenxin.Lib.Business.BLL.ContractRemark bllContractRemark = new Chenxin.Lib.Business.BLL.ContractRemark();

        //合同备注信息
        Chenxin.Lib.Business.Entity.t_Contract_RemarkInfo entContractRemark =
            (Chenxin.Lib.Business.Entity.t_Contract_RemarkInfo)JsonHelp.Convert2Entity(remarkinfo,
                    typeof(Chenxin.Lib.Business.Entity.t_Contract_RemarkInfo));

        entContractRemark = CompleteContractRemark(entContractRemark);
        //合同备注信息录入
        bllContractRemark.Add(entContractRemark);

        Chenxin.Lib.Business.Entity.t_Contract_BaseInfo baseinfo = Chenxin.Lib.Business.DAL.t_Contract_BaseInfo.GetModel(entContractRemark.ContractId);
        string text = CurrentUserInfo.UserName + "：合同备注。成交编号：" + baseinfo.ContractCode;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_BargainRemark"].ToString());
        
        return retExecResult(Constants.ExecResult.Succ, "备注登记成功！", "");
    }

    //*******************************************
    //功能概要       ：作废成交记录跟进
    //参数説明       ：context 请求的上下文
    //返回值説明     ：无
    //功能詳細       ：作废成交记录跟进
    //作成者         ：zou.xiaochun
    //作成日         ：2010-11-01
    //*******************************************
    private string AddCancelRemark(HttpContext context)
    {
        //合同备注信息
        string remarkinfo = context.Request.Params["model"];

        //合同备注信息业务类
        Chenxin.Lib.Business.BLL.ContractRemark bllContractRemark = new Chenxin.Lib.Business.BLL.ContractRemark();

        //合同备注信息
        Chenxin.Lib.Business.Entity.t_Contract_RemarkInfo entContractRemark =
            (Chenxin.Lib.Business.Entity.t_Contract_RemarkInfo)JsonHelp.Convert2Entity(remarkinfo,
                    typeof(Chenxin.Lib.Business.Entity.t_Contract_RemarkInfo));

        entContractRemark = CompleteContractRemark(entContractRemark);
        
        Chenxin.Lib.Business.Entity.t_Contract_BaseInfo baseinfo = Chenxin.Lib.Business.DAL.t_Contract_BaseInfo.GetModel(entContractRemark.ContractId);
        if (baseinfo.Status == -1)
        {
            return "{success:'0',msg:'该记录已作废，作废失败！',data:''}";
        }
        baseinfo.Status = -1;
        baseinfo.UpdateCityId = CurrentUserInfo.CityId;
        baseinfo.UpdateCityName = CurrentUserInfo.CityName;
        baseinfo.UpdateCompanyId = CurrentUserInfo.CompanyId;
        baseinfo.UpdateCompanyName = CurrentUserInfo.CompanyName;
        baseinfo.UpdateDepartmentId = CurrentUserInfo.DepartId;
        baseinfo.UpdateDepartmentName = CurrentUserInfo.DepartName;
        baseinfo.UpdateUserId = CurrentUserInfo.UserId;
        baseinfo.UpdateUserName = CurrentUserInfo.UserName;
        baseinfo.UpdateTime = DateTime.Now;
        //合同备注信息录入
        bllContractRemark.Add(entContractRemark);
        //更新成交状态
        Chenxin.Lib.Business.DAL.t_Contract_BaseInfo.Update(baseinfo);

        string text = CurrentUserInfo.UserName + "：作废成交。成交编号：" + baseinfo.ContractCode;
        AddLog(text, BusinessType.BusinessTypeComboList()["OprType_CancelRemark"].ToString());
        
        return retExecResult(Constants.ExecResult.Succ, "作废成功！", "");
    }

    //*******************************************
    //功能概要       ：填充合同备注信息字段
    //参数説明       ：context 请求上下文
    //返回值説明     ：合同备注信息实体
    //功能詳細       ：填充合同备注信息字段
    //作成者         ：zou.xiaochun
    //作成日         ：2010-11-01
    //*******************************************
    private Chenxin.Lib.Business.Entity.t_Contract_RemarkInfo CompleteContractRemark(Chenxin.Lib.Business.Entity.t_Contract_RemarkInfo entContractRemark)
    {
        try
        {
            entContractRemark.RecordId = Guid.NewGuid();
            entContractRemark.RemarkDate = DateTime.Now;
            entContractRemark.RemarkPersonId = CurrentUserInfo.UserId;
            entContractRemark.RemarkPerson = CurrentUserInfo.UserName;
            return entContractRemark;
        }
        catch (Exception ex)
        {
            return null;
        }
    }
}